//
//  ViewController.m
//  myhuibao
//
//  Created by lijun on 13-2-4.
//  Copyright (c) 2013年 myhuibao. All rights reserved.
//

#import "LoginViewController.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJson.h"
#import "config.h"
@interface LoginViewController ()

@end

@implementation LoginViewController
@synthesize edtLoginId;
@synthesize edtPassword;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    edtLoginId.delegate = self;
    edtPassword.delegate = self;
}

- (void)viewDidUnload
{
    [self setEdtLoginId:nil];
    [self setEdtPassword:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"登陆";
    }
    return self;
}
- (IBAction)doLogin:(id)sender {
    NSURL *url = [NSURL URLWithString:url_login];
    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    [request setPostValue: edtLoginId.text forKey:@"member.mobile"];
    [request setPostValue: edtPassword.text forKey:@"member.password"];
    [request setDelegate: self];
    [request startAsynchronous];
    
    
}
//下面是两个代理函数,当数据请求成功时会调用requestFinished，请求失败时（如网络问题或服务器内部错误）会调用requestFailed
- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *jsonStr = [request responseString];
//    NSLog(@"%@",jsonStr);
    NSDictionary *dic = [jsonStr JSONValue];
    
    NSDictionary *member = [dic valueForKey:@"data"];
//    NSLog(@"%@", member);
    if(member == nil || (id)member == [NSNull null]){
        UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                      message:[dic valueForKey:@"log"]
                                                     delegate:nil
                                            cancelButtonTitle:@"确定"
                                            otherButtonTitles:nil];
        [alert show];
        [alert release];

    }else
    {
        [[Config Instance] saveLogin:member];
        [self.navigationController popViewControllerAnimated:YES];
    }
    // Use when fetching binary data
    //    NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@",error);
}


- (IBAction)doBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];

}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    //    if (theTextField == self.textField) {
    [theTextField resignFirstResponder];
    //    }
    return YES;
}
- (void)dealloc {
    [edtLoginId release];
    [edtPassword release];
    [super dealloc];
}
@end
